/**
 * 验证类
 */
(function(g){
    function validtaor(){
        var _rules = {};
        var _msgs = {};
        function bind(){
            _rules["username"] = /^[a-zA-Z][a-zA-Z0-9_]{5,19}$/;
            _rules["nickname"] = /^[\u4E00-\u9FA5a-zA-Z0-9_]+$/;
            _rules["realname"] = /^[\u4E00-\u9FA5]+$/;
            _rules["date"] = /^(\d{4}\/\d{1,2}\/\d{1,2}|\d{4}-\d{1,2}-\d{1,2})$/;
            _rules["password"] = /^[^\s]{6,20}$/;
            _rules["mobile"] = /^1[0-9]{10}$/;
            _rules["phone"] =  /^(\d{3,4}-)(\d{3}|\d{7,8})(-(\d{3,}))?$/;
            _rules["zipCode"] = /^[0-9]{6}$/;
            _rules["price"] = /^-?[0-9]+(\.[0-9]+)?$/;
            _msgs["username"] = "请使用英文字母、数字、下划线, 以英文字母开头, 长度在6到20个字符之间";
            _msgs["nickname"] = "请使用中英文、数字、下划线,长度在4到20个字符之间";
            _msgs["realname"] = "请使用中英,长度在2到30个字符之间";
            _msgs["date"] = "请输入有效日期";
            _msgs["password"] = "请使用非空字符,长度在6到20个字符之间";
            _msgs["mobile"] = "请输入有效手机号码";
            _msgs["phone"] = "请输入有效电话号码";
            _msgs["zipCode"] = "请输入有效邮编";
        };
        this.isPassword = function(str){
            return _rules["password"].test(str) ? "" : _msgs["password"];
        };
        this.isDate = function(str){
            return _rules["date"].test(str) ? "" : _msgs["date"];
        };
        this.isUsername = function(str){
            return _rules["username"].test(str) ? "" : _msgs["username"];
        };
        this.isNickname = function(str){
            return (_rules["nickname"].test(str) && this.isInRangeLength(str, 4, 20)) ? "" : _msgs["nickname"];
        };
        this.isRealname = function(str){
            return (_rules["realname"].test(str) && this.isInRangeLength(str, 2, 30)) ? "" : _msgs["realname"];
        };
        this.isPhone = function(str){
            return _rules["phone"].test(str) ? "" : _msgs["phone"];
        };
        this.isMobile = function(str){
            return _rules["mobile"].test(str) ? "" : _msgs["mobile"];
        };
        this.isZipCode = function(str){
            return _rules["zipCode"].test(str) ? "" : _msgs["zipCode"];
        };
        this.isPrice = function(str, el){
            //可矫正
            if(!_rules["price"].test(str)){
                $(el).val(isNaN(strstr) ? str : "0.00");
            }else{
                var pos = str.indexOf(".");
                var len = str.length;
                if(pos !== -1){
                    if(pos + 2 < len - 1){
                        $(el).val(str.replace(/^(-?)(0*?)(0|[1-9][0-9]*?)(\.[0-9]{2})([0-9]+?)$/, '$1$3$4'));
                    }else{
                        $(el).val(str.replace(/^(-?)(0*?)(0|[1-9][0-9]*?)(\.[0-9]+?)$/, '$1$3$4') + (pos + 2 == len - 1 ? "" : "0"));
                    }                    
                }else{
                    $(el).val(str.replace(/^(-?)(0+?)(0|[1-9][0-9]*?)$/, '$1$3'));
                }
            }
            return true;
        };
        this.isValdId = function(str){        
            var num = str.toUpperCase();
            //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。  
            if(!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))){  
                return '请输入有效的身份证号';
            }
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            //下面分别分析出生日期和校验位
            var len, re;
            len = num.length;
            if(len == 15) {                
                re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
                var bGoodDay = this.bGoodDay(re,num);
                if(!bGoodDay){
                    return '请输入有效的身份证号';
                } else{                    
                    //将15位身份证转成18位
                    //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
                    var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                    var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                    var nTemp = 0,i;
                    num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);
                    for(i = 0; i < 17; i++){
                        nTemp += num.substr(i, 1) * arrInt[i];
                    }
                    num += arrCh[nTemp % 11];
                    return "";
                }
            } else if(len == 18){                
                re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);              
                var bGoodDay = this.bGoodDay(re,num);
                if(!bGoodDay){
                    return '请输入有效的身份证号！';
                }else{
                    //检验18位身份证的校验码是否正确。
                    //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
                    var valnum;
                    var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                    var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                    var nTemp = 0,i;
                    for(i = 0; i < 17; i++){
                        nTemp += num.substr(i, 1) * arrInt[i];
                    }
                    valnum = arrCh[nTemp % 11];
                    if(valnum != num.substr(17, 1)){
                        return '请输入有效的身份证号！';
                    }
                    return "";
                }
            }
            return "";
        };
        this.bGoodDay = function(re,num){
            //检查生日日期是否正确
            var arrSplit = num.match(re),bGoodDay;
            var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
            bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
            return bGoodDay;
        }
        this.isInRangeLength = function(str, xlen, dlen){
            return str.length >= xlen && str.length <= dlen;
        };
        bind();
    }
    g.mbValidator = new validtaor();
})(window);